--Globals yeah I know they are bad
bgWindowOpen = false;
avnumber = 0;

--Constants
VERSION = "$Rev: 32 $"


AVqq = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0","AceHook-2.1","FuBarPlugin-2.0","AceConsole-2.0")


AVqqOptions = {
	type = 'group',
	args = {
			joinav = {
				type = 'execute',
				name = "Join Alterac Valley",
				desc = 'Increases your chances of getting a raid to join AV',	
				func = "JoinAV",
			},
			AVNumber = {
				type = 'range',
				name = "Alterac Valley Instance Number",
				desc = 'Which Battle ground you want to queue, 0 for the first available',
				get = function()
					return avnumber
				end,
					set = function(v)
						avnumber = v
					end,
					min = 0,
					max = 100,
					step = 1,
					isPercent = false,
			},
			WindowsOpen = {
				type = 'execute',
				name = "Query Raid for Versions ",
				desc = 'Query\'s the raid for the version of AVqq they are running',
				func = function()
						if (IsRaidLeader()) then
							SendAddonMessage("AVqq","whatversion","RAID")
						else
							AVqq:Print("You must be raid leader to preform this function.")
						end
				end,
			},
			LeaveQueue = {
				type = 'execute',
				name = "Make Raid Leave Queue.",
				desc = 'Forces Raid to leave AV queue if up.',
				func = function()
					if (IsRaidLeader()) then
						SendAddonMessage("AVqq","leaveQueue","RAID")
					else
						AVqq:Print("You must be raid leader to preform this function.")
					end
				end,
			},
					
			versionquery = {
				type = 'execute',
				name = "Query Raid if there windows are open",
				desc = 'Query\'s the raid if there windows are open',
				func = "windowsOpen"
			},
		}

}




AVqq.OnMenuRequest = AVqqOptions;
function AVqq:OnTextUpdate()
	self:SetText("AVqq")
end
function AVqq:OnEnable()
	self:RegisterEvent("BATTLEFIELDS_SHOW","BGWindowOpen")
	self:RegisterEvent("BATTLEFIELDS_CLOSED","BGWindowClosed")
	self:RegisterEvent("CHAT_MSG_ADDON", "OnMsgRsv")
	self:Print("AVqq Loading")
end


function AVqq:OnMsgRsv(prefix,msg,dist,sender)
	if (prefix == "AVqq") then
		if (strfind(msg,'joinav ')) then
			--self:Print("Joined, "..msg..", "..dist..", "..sender)
			avnum = tonumber(strsub(msg,8))
			JoinBattlefield(avnum)
			SendAddonMessage("AVqq","joined","RAID")
		elseif (msg == "joined") then
			if (IsRaidLeader()) then
				self:Print(sender.. " is Queued.")
			end
		elseif (strfind(msg,'window ')) then
		--	self:Print("Got To window")
				if (IsRaidLeader()) then
					self:Print(sender .. " has window "..strsub(msg,8))
				end
		elseif (msg == "isWindowOpen") then
			if (bgWindowOpen) then 
				SendAddonMessage("AVqq","window Open")
			else
				SendAddonMessage("AVqq","window Closed")
			end 

		elseif (msg == 'whatversion') then
			SendAddonMessage("AVqq","version "..VERSION, "RAID")
		elseif (strfind(msg,'version ')) then
			if (IsRaidLeader()) then
				self:Print(sender.." is at version: "..strsub(msg,9))
			end
		elseif (msg == 'leaveQueue') then 
			AVqq:LeaveQueue()		
		end

	end
end

function AVqq:BGWindowOpen()
	BGName, blah, blah, blah, blah, blah, blah, blah, blah = GetBattlefieldInfo()
	--self:Print("Open window "..BGName)
	if (BGName == "Alterac Valley") then 
		--self:Print("setting boolean")
		bgWindowOpen = true
	end
end

function AVqq:JoinAV()
	if (IsRaidLeader()) then
		SendAddonMessage("AVqq","joinav "..avnumber,"RAID")
		self:Print("Joining the Raid!")
	else
		self:Print("You must be the raid leader")
	end
end

function AVqq:windowsOpen()
	if(IsRaidLeader()) then
		SendAddonMessage("AVqq","isWindowOpen","RAID")
		self:Print("Checking the Raid for open windows.")
	else
		self:Print("You must be the raid leader")
	end
end

function AVqq:BGWindowClosed()
--	self:Print("Window Closed")
	bgWindowOpen = false
end

function AVqq:LeaveQueue()
	for i=1, MAX_BATTLEFIELD_QUEUES do
		local bgStatus, bgName, bgID = GetBattlefieldStatus(i);
		if (bgName == "Alterac Valley") then
			ShowBattlefieldList(i)
			AcceptBattlefieldPort(i,nil);
			CloseBattlefield();
		end
	end
end